package pers.byxiaobai.levelhealth.data;

import java.util.Collection;
import java.util.HashMap;

import org.bukkit.entity.Player;

import pers.byxiaobai.levelhealth.utils.PlayerHealthUtil;

/** 
* @author byxiaobai
* 玩家数据管理器
*/
public class PlayerDataManager {
	private HashMap<String,PlayerData> playerDataMap=new HashMap<>();//是否添加过血量
	public static PlayerDataManager INSTANCE=new PlayerDataManager();
	public PlayerData getPlayerData(String playerName) {
		PlayerData playerData=playerDataMap.get(playerName);
		if(playerData==null) {
			playerData=new PlayerData(false,playerName);
			playerDataMap.put(playerName, playerData);
			return playerData;
		}
		return playerData;
	}
	public Collection<PlayerData> getAllPlayerData() {
		return playerDataMap.values();
	}
	public PlayerData getPlayerData(Player player) {
		PlayerData playerData=getPlayerData(getPlayerName(player));
		return playerData;
	}
	/**
	 * 给玩家添加血量
	 * @param player
	 * @param value
	 */
	public void addPlayerMaxHealth(Player player,double value) {
		PlayerData playerData=getPlayerData(getPlayerName(player));
		PlayerHealthUtil.setPlayerMaxHealth(player, PlayerHealthUtil.getPlayerMaxHealth(player)+value);
		playerData.addedHealth+=value;
		playerDataMap.put(getPlayerName(player), playerData);
	}
	/**
	 * 设置是否添加过血量
	 * @param player
	 * @param isAddedHealth 是否已添加血量
	 */
	public void setIsAddedHealth(Player player,boolean isAddedHealth) {
		String playerName=getPlayerName(player);
		PlayerData playerData=getPlayerData(playerName);
		playerData.isAddedHealth=isAddedHealth;
		playerDataMap.put(playerName, playerData);
	}
	public boolean isPlayerAddedHealth(Player player) {
		if(player==null)return false;
		return getPlayerData(getPlayerName(player)).isAddedHealth;
	}
	private String getPlayerName(Player player) {
		if(player==null)return "";
		return player.getName().toLowerCase();
	}
}
